package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.service.AdminAdService;
import com.cskaoyan.market.service.impl.AdminAdServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

@WebServlet("/admin/ad/*")
public class AdminAdController extends HttpServlet {
    // AdminUserService adminUserService=new AdminUserServiceImpl();
    AdminAdService adminAdService=new AdminAdServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/ad/", "");
        if("list".equals(op)){
            list(req, resp);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String name = req.getParameter("name");
        String content = req.getParameter("content");
        List<MarketAd> adList = adminAdService.list(page, limit, sort, order,name, content);
        //对于分页查询的这些接口，那么我们需要调用oKlIST方法
        Object o = ResponseUtil.okList(adList);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/ad/", "");
        if("create".equals(op)){
            create(req, resp);
        }else if("delete".equals(op)){
            try {
                delete(req,resp);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else if("update".equals(op)){
            update(req,resp);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketAd marketAd = objectMapper.readValue(requestBody, MarketAd.class);
        int i=adminAdService.update(marketAd);
        Object o = ResponseUtil.ok(i);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException, ParseException {
        String requestBody = req.getReader().readLine();
        Integer id= JacksonUtil.parseInteger(requestBody,"id");
        int i=adminAdService.delete(id);
        Object o = ResponseUtil.ok(i);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String requestBody = req.getReader().readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketAd marketAd = objectMapper.readValue(requestBody, MarketAd.class);
        int i=adminAdService.create(marketAd);
        Object o = ResponseUtil.ok(i);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }
}
